package gov.kqqzyyy.monkey.clinicscreen.mytools;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

import gov.kqqzyyy.monkey.clinicscreen.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class BottomWarnInfo {

    public static String getConfigJSON(Context context) {
        String serverPath = "http://" + context.getResources().getString(R.string.default_server) + File.separator + context.getResources().getString(R.string.config_json);
        return getConfigResult(serverPath);
    }

    private static String getConfigResult(String serverPath) {
        try {
            StringBuilder newVerJSON = new StringBuilder();
            OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).build();
            Request request = new Request.Builder().url(serverPath).build();
            Response response;
            response = okHttpClient.newCall(request).execute();
            if (response.code() == 404) {
                return null;
            } else {
                ResponseBody responseBody = response.body();
                if (responseBody != null) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody.byteStream(), StandardCharsets.UTF_8), 8192);
                    String line;
                    while ((line = reader.readLine()) != null) {  //按行读取文件
                        newVerJSON.append(line).append("\n");
                    }
                    reader.close();
                }
                return newVerJSON.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
